package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.mapper.KingBaseBankMapper;
import com.ly.heart.service.IKingBaseBankService;
import com.ly.heart.util.ChineseToPinyin;
import com.ly.heart.util.CurrentUserUtil;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBaseBankEntity;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 金蝶-基础数据-银行Service业务层处理
 * @author wutao
 * @date 2024-07-04
 */
@Service
public class KingBaseBankServiceImpl extends ServiceImpl<KingBaseBankMapper, KingBaseBankEntity> implements IKingBaseBankService {

    @Resource
    private KingBaseBankMapper kingBaseBankEntityMapper;

    /**
     * 查询金蝶-基础数据-银行
     * @param id 金蝶-基础数据-银行ID
     * @return 金蝶-基础数据-银行
     */
    @Override
    public KingBaseBankEntity selectKingBaseBankEntityById (String id) {
        return kingBaseBankEntityMapper.selectKingBaseBankEntityById ( id );
    }

    /**
     * 查询金蝶-基础数据-银行列表
     * @param kingBaseBankEntity 金蝶-基础数据-银行
     * @return 金蝶-基础数据-银行
     */
    @Override
    public Page selectKingBaseBankEntityList (KingBaseBankEntity kingBaseBankEntity) {
        LambdaQueryWrapper<KingBaseBankEntity> lambdaQueryWrapper = new LambdaQueryWrapper<> ();
        if ( StringUtils.isNotBlank ( kingBaseBankEntity.getCode () ) ) {
            lambdaQueryWrapper.eq ( KingBaseBankEntity :: getCode, kingBaseBankEntity.getCode () );
        }
        lambdaQueryWrapper.eq ( KingBaseBankEntity :: getIsUsed, ConstUtil.IS_USED_YES );
        Page page = this.page ( new Page<> ( kingBaseBankEntity.getPage (), kingBaseBankEntity.getLimit () ), lambdaQueryWrapper );
        return page;
    }

    /**
     * 新增金蝶-基础数据-银行
     * @param kingBaseBankEntity 金蝶-基础数据-银行
     * @return 结果
     */
    @Override
    public int insertKingBaseBankEntity (KingBaseBankEntity kingBaseBankEntity) {
        kingBaseBankEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ( new LambdaQueryWrapper<KingBaseBankEntity> ().like ( KingBaseBankEntity :: getTitle, kingBaseBankEntity.getTitle () ) );
        String firstLetter = ChineseToPinyin.getFirstLetter ( kingBaseBankEntity.getTitle () );
        kingBaseBankEntity.setCode ( firstLetter.concat ( String.format ( "%08d", count ) ) );
        kingBaseBankEntity.setDeptId ( CurrentUserUtil.getCompanyId () );
        kingBaseBankEntity.setCreateTime ( DateUtils.getNowDate () );
        return kingBaseBankEntityMapper.insertKingBaseBankEntity ( kingBaseBankEntity );
    }

    /**
     * 修改金蝶-基础数据-银行
     * @param kingBaseBankEntity 金蝶-基础数据-银行
     * @return 结果
     */
    @Override
    public int updateKingBaseBankEntity (KingBaseBankEntity kingBaseBankEntity) {
        kingBaseBankEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseBankEntity.setUpdateTime ( DateUtils.getNowDate () );
        return kingBaseBankEntityMapper.updateKingBaseBankEntity ( kingBaseBankEntity );
    }

    /**
     * 批量删除金蝶-基础数据-银行
     * @param ids 需要删除的金蝶-基础数据-银行ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseBankEntityByIds (String[] ids) {
        List<KingBaseBankEntity> list = this.list ( new LambdaQueryWrapper<KingBaseBankEntity> ().in ( KingBaseBankEntity :: getId, ids ) );
        //可删除ids集
        List<String> idList = list.stream ().filter ( bank -> bank.getIsDel () == 1 ).map ( KingBaseBankEntity :: getId ).collect ( Collectors.toList () );
        //不可删除的银行名称集合
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( baseBank -> {
            if ( baseBank.getIsDel () == 2 ) {
                stringBuilder.append ( baseBank.getTitle () + " " );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseBankEntity> ().
                set ( KingBaseBankEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingBaseBankEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseBankEntity :: getUpdateTime, DateUtils.getNowDate () ).
                in ( KingBaseBankEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除银行失败！" );
        }
        String str = "删除银行成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-基础数据-银行信息
     * @param id 金蝶-基础数据-银行ID
     * @return 结果
     */
    @Override
    public AjaxResult disabled (String id) {
        KingBaseBankEntity bankEntity = this.getById ( id );
        if ( bankEntity == null ) {
            throw new CustomException ( "无此银行！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseBankEntity> ().
                set ( KingBaseBankEntity :: getIsDisabled, bankEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingBaseBankEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseBankEntity :: getUpdateTime, DateUtils.getNowDate () ).
                eq ( KingBaseBankEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( bankEntity.getIsDisabled () == 1 ? "禁用银行失败！" : "解禁银行失败！" );
        }
        return AjaxResult.success ();
    }
}
